package com.ys.everydaynews.utils

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.ys.everydaynews.R

object ImageLoaderUtil {
    /**
     * 加载图片String地址
     * */
    fun loadImage(context: Context, url: String, imageView: ImageView) {
        var strategy = DiskCacheStrategy.NONE
        if (url.startsWith("http://") || url.startsWith("https://")) {
            strategy = DiskCacheStrategy.RESOURCE
        }
        val options = RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
            .dontAnimate().diskCacheStrategy(strategy)
        Glide.with(context).load(url).apply(options).into(imageView)
    }

    /**
     * 加载图片(uri)
     */
    fun loadImage(context: Context, uri: Uri, imageView: ImageView) {
        var strategy = DiskCacheStrategy.RESOURCE
        val options =
            RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                .dontAnimate().diskCacheStrategy(strategy)
        Glide.with(context).load(uri).apply(options).into(imageView)
    }

    /**
     * 加载图片(bitmap)
     */
    fun loadImage(context: Context, bitmap: Bitmap, imageView: ImageView) {
        var strategy = DiskCacheStrategy.RESOURCE
        val options =
            RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                .dontAnimate().diskCacheStrategy(strategy)
        Glide.with(context).load(bitmap).apply(options).into(imageView)
    }

    /**
     * 加载图片(String地址)---指定宽高
     */
    fun loadImage(
        context: Context,
        url: String,
        imageView: ImageView,
        width: Int,
        height: Int
    ) {
        var strategy = DiskCacheStrategy.NONE
        if (url.startsWith("http://") || url.startsWith("https://")) {
            strategy = DiskCacheStrategy.RESOURCE
        }
        val options =
            RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                .dontAnimate().override(width, height).diskCacheStrategy(strategy)
        Glide.with(context).load(url).apply(options).into(imageView)
    }

    /**
     * 加载圆形图片
     */
    fun loadCircleImage(context: Context, url: String, imageView: ImageView) {
        var strategy = DiskCacheStrategy.NONE
        if (url.startsWith("http://") || url.startsWith("https://")) {
            strategy = DiskCacheStrategy.RESOURCE
        }
        val options = RequestOptions().centerCrop().circleCrop().placeholder(R.color.white)
            .error(R.color.white).dontAnimate().diskCacheStrategy(strategy)
        Glide.with(context).load(url).apply(options).into(imageView)

    }
}